package com.hucii.app.service;

import java.util.Observable;

import com.hucii.app.MyConstans;
import com.hucii.app.cache.PictureCache;
import com.hucii.app.cache.PictureRequest.ILoadImageListener;

import android.content.Context;
import android.graphics.Bitmap;


/**
 * 远程资源管理类，一般用来拉取图像用的
 * 
 * 
 */
public class RemotePictureManager extends Observable implements ILoadImageListener {

	private Context mContext;
	private PictureCache picCache;

	public void onGetPictureSuccessed(String picUrl, Bitmap image) {
		Request req = new Request(picUrl);
		req.success = true;
		req.image = image;
		notifyObservers(req);
	}

	public void onGetPictureFail(String picUrl) {
		Request req = new Request(picUrl);
		req.success = false;
		notifyObservers(req);
	}

	public RemotePictureManager(Context context) {
		mContext = context;
		picCache = PictureCache.getInstance(mContext);
	}

	public PictureCache getPicCache() {
		return picCache;
	}

	public Bitmap getPicture(String url) {
		return picCache.getPicture(this, MyConstans.PIC_SIZE_SMALL, url);
	}

	@Override
	public void notifyObservers(Object data) {
		setChanged();
		super.notifyObservers(data);
	}

	public static class Request {
		public String url;
		public boolean success = true;

		public Bitmap image = null;

		public Request(String url) {
			this.url = url;
		}

		@Override
		public boolean equals(Object o) {
			Request request = (Request) o;
			return url.equals(request.url);
		}

		@Override
		public int hashCode() {
			return url.hashCode();
		}
	}
}
